home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / fractals / mandelsquare / mandelsquare-1.06.lha / Packer.c < prev    next >
Text File  |  1992-10-12  |  3KB  |  192 lines

  1. /*
  2. **    MandelSquare - AmigaDOS 2.0/3.0 Mandelbrot set explorer
  3. **
  4. **    Packer.c, CmpByteRun compression and decompression routines
  5. **
  6. **    Copyright © 1991-1992 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10.     /* Packer modes. */
  11.  
  12. #define DUMP    0
  13. #define RUN    1
  14.  
  15.     /* Minimum data run size, maximum data run size and maximum cache size. */
  16.  
  17. #define MINRUN    3
  18. #define MAXRUN    128
  19. #define MAXDAT    128
  20.  
  21.     /* Number of packed bytes and pack buffer. */
  22.  
  23. STATIC LONG    PackedBytes;
  24. STATIC BYTE    Buffer[MAXDAT + 1];
  25.  
  26.     /* PutDump(register BYTE *Destination,register LONG Count):
  27.      *
  28.      *    Flush out the data dump.
  29.      */
  30.  
  31. STATIC BYTE * __regargs
  32. PutDump(register BYTE *Destination,register LONG Count)
  33. {
  34.     register BYTE *Source = Buffer;
  35.  
  36.     *Destination++     = Count - 1;
  37.      PackedBytes    += Count + 1;
  38.  
  39.     while(Count--)
  40.         *Destination++ = *Source++;
  41.  
  42.     return(Destination);
  43. }
  44.  
  45.     /* PutRun(register BYTE *Destination,LONG Count,WORD Char):
  46.      *
  47.      *    Flush out a run of data bytes.
  48.      */
  49.  
  50. STATIC BYTE * __regargs
  51. PutRun(register BYTE *Destination,LONG Count,WORD Char)
  52. {
  53.     *Destination++     = -(Count - 1);
  54.     *Destination++     = Char;
  55.      PackedBytes    += 2; 
  56.  
  57.     return(Destination);
  58. }
  59.  
  60.     /* PackRow(BYTE **SourcePtr,register BYTE *Destination,LONG RowSize):
  61.      *
  62.      *    Compress a single row of bytes.
  63.      */
  64.  
  65. LONG __regargs
  66. PackRow(BYTE **SourcePtr,register BYTE *Destination,LONG RowSize)
  67. {
  68.     register BYTE *Source = *SourcePtr;
  69.  
  70.     WORD    Buffered    = 1,
  71.         RunStart    = 0;
  72.     BYTE    Mode        = DUMP,
  73.         LastChar,
  74.         Char;
  75.  
  76.     PackedBytes = 0;
  77.  
  78.     Buffer[0] = LastChar = Char = *Source++;
  79.  
  80.     RowSize--;
  81.  
  82.     while(RowSize--)
  83.     {
  84.         Buffer[Buffered++] = Char = *Source++;
  85.  
  86.         if(Mode)
  87.         {
  88.             if((Char != LastChar) || (Buffered - RunStart > MAXRUN))
  89.             {
  90.                 Destination    = PutRun(Destination,Buffered - 1 - RunStart,LastChar);
  91.                 Buffer[0]    = Char;
  92.                 Buffered    = 1;
  93.                 RunStart    = 0;
  94.                 Mode        = DUMP;
  95.             }
  96.         }
  97.         else
  98.         {
  99.             if(Buffered > MAXDAT)
  100.             {
  101.                 Destination    = PutDump(Destination,Buffered - 1);
  102.                 Buffer[0]    = Char;
  103.                 Buffered    = 1;
  104.                 RunStart    = 0;
  105.             }
  106.             else
  107.             {
  108.                 if(Char == LastChar)
  109.                 {
  110.                     if(Buffered - RunStart >= MINRUN)
  111.                     {
  112.                         if(RunStart)
  113.                             Destination = PutDump(Destination,RunStart);
  114.  
  115.                         Mode = RUN;
  116.                     }
  117.                     else
  118.                     {
  119.                         if(!RunStart)
  120.                             Mode = RUN;
  121.                     }
  122.                 }
  123.                 else
  124.                     RunStart = Buffered - 1;
  125.             }
  126.         }
  127.  
  128.         LastChar = Char;
  129.     }
  130.  
  131.     if(Mode)
  132.         PutRun(Destination,Buffered - RunStart,LastChar);
  133.     else
  134.         PutDump(Destination,Buffered);
  135.  
  136.     *SourcePtr = Source;
  137.  
  138.     return(PackedBytes);
  139. }
  140.  
  141.     /* UnPackRow(BYTE **SourcePtr,BYTE **DestinationPtr,register WORD DestinationBytes):
  142.      *
  143.      *    Unpack CmpByteRun compressed data.
  144.      */
  145.  
  146. VOID __regargs
  147. UnPackRow(BYTE **SourcePtr,BYTE **DestinationPtr,register WORD DestinationBytes)
  148. {
  149.     register BYTE    *Source        = *SourcePtr,
  150.             *Destination    = *DestinationPtr,
  151.             Char;
  152.     register WORD    Count;
  153.  
  154.     while(DestinationBytes > 0)
  155.     {
  156.         if((Count = *Source++) >= 0)
  157.         {
  158.             Count++;
  159.  
  160.             if((DestinationBytes -= Count) < 0)
  161.                 break;
  162.             else
  163.             {
  164.                 do
  165.                     *Destination++ = *Source++;
  166.                 while(--Count);
  167.             }
  168.         }
  169.         else
  170.         {
  171.             if(Count != (WORD)(-128))
  172.             {
  173.                 Count = -Count + 1;
  174.  
  175.                 if((DestinationBytes -= Count) < 0)
  176.                     break;
  177.                 else
  178.                 {
  179.                     Char = *Source++;
  180.  
  181.                     do
  182.                         *Destination++ = Char;
  183.                     while(--Count);
  184.                 }
  185.             }
  186.         }
  187.     }
  188.  
  189.     *SourcePtr        = Source;
  190.     *DestinationPtr        = Destination;
  191. }
  192.